package com.cyandpos.pad.pay.model;

import android.text.TextUtils;
import android.util.Log;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.netdata.R_Addpay;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.QueryMemberInfoResponse;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.RefundOrder;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.log.MessageLog;
import com.google.gson.JsonObject;

import java.util.List;

/**
 * Created by zx on 2017/3/23.
 */

public class IPayNetRequestModelImpl implements IPayNetRequestModel {

    @Override  //同步付款modle接口
    public void loadAddpayInfo(String orderNo, List<Payment> payItems, int saleType, boolean clearFlag, final AddpayRequestListener listener) {
        PosHttpManager.AddPay(orderNo, payItems,saleType,clearFlag, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject res) {
                Log.i("TAG----同步付款",res.toString());
                BaseResponse<R_Addpay> baseResponse = BaseResponse.fromJson(res.toString(),R_Addpay.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        listener.getAddpayInfoSuccess(baseResponse.getData());
                    }catch (Exception e){
                        listener.getAddpayInfoFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.getAddpayInfoFailure(baseResponse.getMsg());
                    }else {
                        listener.getAddpayInfoFailure(baseResponse.getTips());
                    }

                }
            }
            @Override
            public void onFailure(String msg) {
                listener.getAddpayInfoFailure(msg);
            }
        });
    }

    @Override   //完成订单接口
    public void loadCommitPayInfo(TicketInfo mTicket,final boolean isCancelAfterSwipe,final CommitPayRequestListener listener) {
        PosHttpManager.CommitPay(mTicket,isCancelAfterSwipe, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject res) {

                BaseResponse<TicketInfo> baseResponse = BaseResponse.fromJson(res.toString(),TicketInfo.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        listener.getCommitPayInfoSuccess(baseResponse.getData(),isCancelAfterSwipe);
                    }catch (Exception e){
                        listener.getCommitPayInfoFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.getCommitPayInfoFailure(baseResponse.getMsg());
                    }else {
                        listener.getCommitPayInfoFailure(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.getCommitPayInfoFailure(msg);
            }
        });
    }

    @Override
    public void loadGetVipCardToGrantVoucher(QueryMemberRequest requestParam, final LoadGetVipCardToGrantVoucherListener listener) {
        PosHttpManager.GetVipCardToGrantVoucher(requestParam, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject res) {

                BaseResponse<QueryMemberInfoResponse> baseResponse = BaseResponse.fromJson(res.toString(),QueryMemberInfoResponse.class);
                if (baseResponse.getCode().equals("0")) {
                    try {
                        QueryMemberInfoResponse backM= baseResponse.getData();
                        Member member = new Member();
                        member.setMemberCardId(backM.getCardId());
                        member.setMemberCardNo(backM.getCardCode());
                        member.setMemberCardTypeId(backM.getCardTypeId());
                        member.setMemberCardTypeName(backM.getCardTypeName());
                        member.setMemberCardValidCent(backM.getValidCent()+"");
                        member.setMemberName(backM.getVipName());
                        listener.getloadGetVipCardToGrantVoucherSuccess(member);
                    }catch (Exception e){
                        listener.getloadGetVipCardToGrantVoucherFailure("格式错误" + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.getloadGetVipCardToGrantVoucherFailure(baseResponse.getMsg());
                    }else {
                        listener.getloadGetVipCardToGrantVoucherFailure(baseResponse.getTips());
                    }
                    
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.getloadGetVipCardToGrantVoucherFailure(msg);
            }
        });
    }

    @Override
    public void queryDepositInfo(String depositNo,final QueryDepositInfoListener listener) {
        QueryOrderReq req = new QueryOrderReq();
        req.setSktno(ErpCommonData.sktno);
        req.setOrderNo(depositNo);

        PosHttpManager.QueryOrderInfoEexcute(req, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                try {
                    BaseResponse<RefundOrder> baseResponse = BaseResponse.fromJson(response.toString(), RefundOrder.class);
                    if (baseResponse.getCode().equals("0")){
                        RefundOrder depositInfo = baseResponse.getData();
                        if(depositInfo.getOrderList().size()>0){
                            listener.getDepositInfoSuccess(depositInfo);
                        }else{
                            onFailure("查询订金单返回为空！");
                        }


                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            onFailure(baseResponse.getMsg());
                        }else {
                            onFailure(baseResponse.getTips());
                        }
                    }
                }catch (Exception e){
                    onFailure("接口异常："+e.toString());
                    MessageLog.writeLog("接口异常："+e.toString());
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.getDepositInfoFailure(msg+"......");
            }
        });

    }


}
